package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"strings"
)

func main() {
	// 作为客户端监听Ip和端口
	conn, err := net.Dial("tcp", "127.0.0.1:20000")
	if err != nil {
		fmt.Println("err:", err)
		return
	}
	defer conn.Close()
	// 监听命令行输入
	reader := bufio.NewReader(os.Stdin)
	for {
		input, _ := reader.ReadString('\n')
		inputInfo := strings.Trim(input, "\r\n")
		if strings.ToUpper(inputInfo) == "Q" {
			return
		}
		// 发送数据
		_, err := conn.Write([]byte(inputInfo))
		if err != nil {
			return
		}
		// 接受服务端的数据
		receiveData := make([]byte, 128)
		n, err := conn.Read(receiveData)
		if err != nil {
			fmt.Println("recv failed, err:", err)
			return
		}
		fmt.Println("收到服务端消息：", string(receiveData[:n]))
	}
}
